package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.Node;
import com.heima.pojo.Region;
import com.heima.service.NodeService;
import com.heima.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {


    @Autowired
    private NodeService nodeServiceService;


    @Autowired
    private RegionService regionService;

    //删除区域
    @Override
    public boolean delayRegionIfNotNode(Long id) {

        if(id==null){
            return false;
//            throw new BusinessException("你的参数有误");
        }
        LambdaQueryWrapper<Node> qw = new LambdaQueryWrapper<>();
        qw.eq(Node::getRegionId,id);
        int count = nodeServiceService.count(qw);
        //判断区域里面有没有点位
        if (count>0){
            return false;
//            throw new BusinessException("区域里面存在点位，无法删除");
        }else {
            //进行删除
            boolean flag = regionService.removeById(id);
            //删除成功
            if (flag) {
                return true;
            }
            //删除失败
            return false;
        }
    }
}
